#include<allegro.h>
#include<stdlib.h>
#include<stdio.h>

BITMAP *img;
BITMAP *buffer;
FILE *pimg;
volatile int tempo = 0;
//void blit(BITMAP *src, BITMAP *dest, int s_x, int s_y, int d_x, int d_y, int w, int h)

void inc_tempo(){
   tempo++;
}

int main () {

	int img_x , img_y , img_lado , n , count = 0;
	char nome[100], delay_to_s[50];
	int delay = 100;
	int c_tempo = 0, teclado_tempo = 0;
   
	allegro_init();
	install_timer();
	install_keyboard();
	set_color_depth(24);
	set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
	buffer = create_bitmap(800, 600);

	pimg = fopen("imagem.txt", "r");
   
	install_int(inc_tempo, 1);
   
	fscanf(pimg, "%s %d", nome, &n);
   
	img = load_bitmap( nome , NULL);
	
	img_x = img->w;
	img_y = img->h;
	img_lado = img_x/n;

	while(!key[KEY_ESC]) {
	
		acquire_screen();
      
		c_tempo += tempo;
      
		teclado_tempo += tempo;
	      
		tempo = 0;
	
		if (c_tempo > delay) {
			count++;
			c_tempo = 0;
		}
	      
		if (teclado_tempo > 50) {
			if (key[KEY_UP]) delay--;
			if (key[KEY_DOWN]) delay++;                                                    
			if (delay < 5)
				delay = 5;
			teclado_tempo = 0;
		}
	      
		clear_to_color(buffer, makecol(255, 255, 255));
      
		sprintf(delay_to_s, "%d", delay);
	      
		textout_ex( buffer, font, delay_to_s, 50, 50, makecol( 255, 255, 255), makecol( 0, 0, 0) );
	      
		masked_blit(img, buffer, (img_lado * (count%n) ), 0, (800 - img_x)/2, (600 - img_y)/2, img_lado, img_y);
	      
		draw_sprite(screen, buffer, 0, 0);
      
		release_screen();
	}
	return 0;
}
END_OF_MAIN();

